package cn.edu.sgu.www.mhxysy.controller.system;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.entity.system.Table;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.system.TableService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "数据库表管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/table", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class TableController {

	private final TableService tableService;

	@Autowired
	public TableController(TableService tableService) {
		this.tableService = tableService;
	}

	@ApiOperation("查询所有数据库表")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Table>> selectAll() {
		List<Table> list = tableService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询数据库所有表的名称")
	@RequestMapping(value = "/getTableNames", method = RequestMethod.GET)
	public JsonResult<List<String>> getTableNames() throws IOException {
		List<String> list = tableService.getTableNames();

		return JsonResult.success("扫描到项目entity包下的实体类", list);
	}

	@ApiOperation("根据实体类生成创建表的SQL语句")
	@RequestMapping(value = "/generate", method = RequestMethod.GET)
	public JsonResult<String> generate(
			@ApiParam(name = "className", value = "类的全限定名", required = true)
			@NotBlank(message = "类的全限定名" + CustomMessages.NOT_NULL) String className) throws ClassNotFoundException {
		String generate = tableService.generate(className);

		return JsonResult.success(null, generate);
	}

}